#include "PrecompiledHeader.h"
#include "ConfigurationPanels.h"
#include "System.h"
/*
using namespace pxSizerFlags;
static int pxGetTextHeight( const wxWindow* wind, int rows )
{
	wxClientDC dc(wx_const_cast(wxWindow *, wind));
	dc.SetFont( wind->GetFont() );
	return (dc.GetCharHeight() + 1 ) * rows;
}
/*const wxChar* Panels::ValDanX_KERNELPanel::GetNTSCcycleSliderMsg( int val )
{
	switch( val )
	{
		case 0:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX0",
				L"0"
			);
		case 1:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX1",
				L"1"
			);

		case 2:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX2",
				L"2"
			);

		case 3:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX3",
				L"3"
			);
		case 4:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX4",
				L"4"
			);
		case 5:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"5"
			);
		case 6:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"6"
			);
		case 7:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"7"
			);
		case 8:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"8"
			);
		case 9:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"9"
			);
		case 10:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"10"
			);
		case 11:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"11"
			);
		case 12:
			return pxE( ".Panel:ValDanX_Speedhacks:NTSCCycleX5",
				L"12"
			);

		default:
            break;
	}

	return L"Unreachable Warning Suppressor!!";
}

const wxChar* Panels::ValDanX_KERNELPanel::GetPALcycleSliderMsg( int val )
{
	switch( val )
	{
		case 0:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleStealOff",
				L"0"
			);

		case 1:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal1",
				L"1"
			);

		case 2:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal2",
				L"2"
			);

		case 3:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal3",
				L"3"
			);
		case 4:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"4"
			);
		case 5:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"5"
			);

		case 6:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"6"
			);

		case 7:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"7"
			);

		case 8:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"8"
			);

		case 9:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"9"
			);

		case 10:
			return pxE( ".Panel:ValDanX_Speedhacks:PALCycleSteal4",
				L"10"
			);


        default:
            break;
	}

	return L"Unreachable Warning Suppressor!!";
}*//*
const wxChar* Panels::ValDanX_KERNELPanel::GetRegionMODESliderMsg( int val )
{
	switch( val )
	{
		case 0:
			return pxE( ".Panel:ValDanX_Speedhacks:RegionMODE0",
				L"NTSC"
			);
			case 1:
			return pxE( ".Panel:ValDanX_Speedhacks:RegionMODE1",
				L"Auto"
			);
			case 2:
			return pxE( ".Panel:ValDanX_Speedhacks:RegionMODE2",
				L"PAL"
			);
	default:
     break;
	}
	return L"Unreachable Warning Suppressor!!";
}
Panels::ValDanX_KERNELPanel::ValDanX_KERNELPanel( wxWindow* parent ) :
	BaseApplicableConfigPanel( parent )
{
	wxStaticBoxSizer& groupSizer = *new wxStaticBoxSizer( wxVERTICAL, this, _("Kernel") );
	struct CheckTextMess
	{
		wxString label, tooltip;
	};

	const wxSizerFlags sliderFlags( wxSizerFlags().Border( wxLEFT | wxRIGHT, 8 ).Expand() );
	wxPanelWithHelpers* right	= new wxPanelWithHelpers( this, wxVERTICAL );
	wxPanelWithHelpers* center	= new wxPanelWithHelpers( this, wxVERTICAL );
	wxPanelWithHelpers* center2	= new wxPanelWithHelpers( this, wxVERTICAL );
	wxPanelWithHelpers* left	= new wxPanelWithHelpers( this, wxVERTICAL );
left->SetMinWidth( 200 );
right->SetMinWidth( 200 );
center->SetMinWidth( 200 );
center2->SetMinWidth( 600 );

	// ------------------------------------------------------------------------

	/*wxPanelWithHelpers* NTSCSliderPanel = new wxPanelWithHelpers( left, wxVERTICAL, _("ValDanX NTSC speed upper") );

	m_slider_NTSCcycle = new wxSlider( NTSCSliderPanel, wxID_ANY, 0, 0, 12,
		wxDefaultPosition, wxDefaultSize, wxHORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS );

	m_msg_NTSCcycle = new pxStaticHeading( NTSCSliderPanel );
	m_msg_NTSCcycle->SetForegroundColour( wxColour( L"Red" ) );
	m_msg_NTSCcycle->SetMinSize( wxSize( wxDefaultCoord, pxGetTextHeight(m_msg_NTSCcycle, 3) ) );

	const wxChar* NTSC_tooltip = pxE( ".Tooltip:ValDanX_KERNELPanel:NTSCCycleRate Slider",
		L"Most speed up"
	);

	pxSetToolTip( m_slider_NTSCcycle, NTSC_tooltip );
	pxSetToolTip( m_msg_NTSCcycle, NTSC_tooltip );*//*
	// ------------------------------------------------------------------------

	wxPanelWithHelpers* RegionMODES = new wxPanelWithHelpers( center2, wxVERTICAL, _("Select region") );

	m_slider_RegionMODES = new wxSlider( RegionMODES, wxID_ANY, 0, 0, 2,
		wxDefaultPosition, wxDefaultSize, wxHORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS );

	m_msg_RegionMODES = new pxStaticHeading( RegionMODES );
	m_msg_RegionMODES->SetForegroundColour( wxColour( L"Red" ) );
	m_msg_RegionMODES->SetMinSize( wxSize( wxDefaultCoord, pxGetTextHeight(m_msg_RegionMODES, 3) ) );

	const wxChar* RegionMODES_tooltip = pxE( ".Tooltip:ValDanX_KERNELPanel:RegionMODES Slider",
		L"Select region"
	);

	pxSetToolTip( m_slider_RegionMODES, RegionMODES_tooltip );
	pxSetToolTip( m_msg_RegionMODES, RegionMODES_tooltip );

	// ------------------------------------------------------------------------

	/*wxPanelWithHelpers* PALSliderPanel = new wxPanelWithHelpers( right, wxVERTICAL, _("ValDanX PAL speed upper") );

	m_slider_PALstealer = new wxSlider( PALSliderPanel, wxID_ANY, 0, 0, 10, wxDefaultPosition, wxDefaultSize,
		wxHORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS );

	m_msg_PALstealer = new pxStaticHeading( PALSliderPanel );
	m_msg_PALstealer->SetForegroundColour( wxColour( L"Red" ) );
	m_msg_PALstealer->SetMinSize( wxSize( wxDefaultCoord, pxGetTextHeight(m_msg_PALstealer, 3) ) );

	const wxChar* PAL_tooltip = pxE( ".Tooltip:ValDanX_KERNELPanel:PALCycleStealing Slider",
		L"Most speed up"
	);

	pxSetToolTip( m_slider_PALstealer, PAL_tooltip );
	pxSetToolTip( m_msg_PALstealer, PAL_tooltip );*//*

	// ------------------------------------------------------------------------
	//  Layout and Size ---> (!!)

	//*NTSCSliderPanel	+= m_slider_NTSCcycle		| sliderFlags;
	//*NTSCSliderPanel	+= m_msg_NTSCcycle	    	| sliderFlags;

	*RegionMODES	+= m_slider_RegionMODES		| sliderFlags;
	*RegionMODES	+= m_msg_RegionMODES	    | sliderFlags;

	//*PALSliderPanel	+= m_slider_PALstealer	| sliderFlags;
	//*PALSliderPanel	+= m_msg_PALstealer		| sliderFlags;

	//*left	+= NTSCSliderPanel	| StdExpand();
	*center2	+= RegionMODES  	| pxExpand;
	//*right	+= PALSliderPanel	| StdExpand();
	//*right	+= StdPadding;

	s_table = new wxFlexGridSizer( 2 );
	s_table ->AddGrowableCol( 0, 1 );
	s_table ->AddGrowableCol( 1, 1 );


	const CheckTextMess check_text[NUM_OF_VALDANX_KERNEL] =
	{
		{
			_("Enable IOP kernel"),
			wxEmptyString
		},
		{
			_("Disable new EE Branch kernel [NOT RECOMMENDED] "),
			wxEmptyString
		},
		{
			_("Enable ASM code kernel (speed up) [RECOMMENDED] "),
			wxEmptyString
		},
		{
			_("Enable MMX helper (speed up) [RECOMMENDED] "),
			wxEmptyString
		},
		{
			_("Enable recDI FlushCall (iCOP0)(speed up) [RECOMMENDED] "),
			wxEmptyString
		},
		{
			_("Enable add MMX code (speed up) [RECOMMENDED] "),
			wxEmptyString
		}
	};


wxButton* SpeedStable    = new wxButton( left, wxID_ANY, _("Default Speed Stable")    );
wxButton* SpeedUnStable  = new wxButton( left, wxID_ANY, _("Default Speed UnStable")  );
wxButton* SpeedWarning   = new wxButton( left, wxID_ANY, _("Default Speed Warning")   );

	for( int i=0; i<NUM_OF_VALDANX_KERNEL; ++i )
	{
		groupSizer += (m_checkbox[i] = new pxCheckBox( this, check_text[i].label ));
		m_checkbox[i]->SetToolTip( check_text[i].tooltip );
	}
*left   += SpeedStable              | pxExpand;
*left += SpeedUnStable            | pxExpand;
*left  += SpeedWarning             | pxExpand;
*this	+= groupSizer       		| pxCenter;
*this	+= new wxStaticLine( this )	| pxExpand.Border(wxLEFT | wxRIGHT, 20);
*this	+= StdPadding;
*this   += center2       			| pxExpand;
*this	+= new wxStaticLine( this )	| pxExpand;
//*this	+= s_table					| pxExpand;
*this  += 20;
*this+= left			         	| pxExpand;
*this+= right			        | pxExpand;
*this+= center			        | pxExpand;
//*this   += left;
	Connect( wxEVT_SCROLL_PAGEUP,	wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );
	Connect( wxEVT_SCROLL_PAGEDOWN,	wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );
	Connect( wxEVT_SCROLL_LINEUP,	wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );
	Connect( wxEVT_SCROLL_LINEDOWN,	wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );
	Connect( wxEVT_SCROLL_TOP,		wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );
	Connect( wxEVT_SCROLL_BOTTOM,	wxScrollEventHandler( ValDanX_KERNELPanel::Slider_Click ) );

Connect( SpeedStable   -> GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ValDanX_KERNELPanel::AppStatusEvent_DefaultSpeed  ) );
Connect( SpeedUnStable -> GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ValDanX_KERNELPanel::AppStatusEvent_DefaultUnSpeed) );
Connect( SpeedWarning  -> GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ValDanX_KERNELPanel::AppStatusEvent_DefaultWarning) );

//Connect( GSStable->GetId(),      wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(ValDanX_GSPanel::GSreset)   );

	//Connect( m_slider_NTSCcycle->GetId(),		wxEVT_SCROLL_CHANGED, wxScrollEventHandler( ValDanX_KERNELPanel::NTSCCycleRate_Scroll ) );
	//Connect( m_slider_PALstealer->GetId(),  	wxEVT_SCROLL_CHANGED, wxScrollEventHandler( ValDanX_KERNELPanel::PALCycleRate_Scroll ) );
	Connect( m_slider_RegionMODES->GetId(),  	wxEVT_SCROLL_CHANGED, wxScrollEventHandler( ValDanX_KERNELPanel::RegionMODE_Scroll ) );
}
/*void Panels::ValDanX_KERNELPanel::SetNTSCcycleSliderMsg()
{
	m_msg_NTSCcycle->SetLabel( GetNTSCcycleSliderMsg(m_slider_NTSCcycle->GetValue()) );
}

void Panels::ValDanX_KERNELPanel::SetPALcycleSliderMsg()
{
	m_msg_PALstealer->SetLabel( GetPALcycleSliderMsg(m_slider_PALstealer->GetValue()) );
}*//*
void Panels::ValDanX_KERNELPanel::SetRegionMODESliderMsg()
{
	m_msg_RegionMODES->SetLabel( GetRegionMODESliderMsg(m_slider_RegionMODES->GetValue()) );
}
void Panels::ValDanX_KERNELPanel::AppStatusEvent_OnSettingsApplied( const Pcsx2Config::ValDanX_KERNELOptions& opts )
{
Pcsx2Config::GSOptions& gsconf( g_Conf->EmuOptions.GS );
Pcsx2Config::SpeedhackOptions& SHconf( g_Conf->EmuOptions.Speedhacks );
	//m_slider_NTSCcycle	->SetValue( opts.NTSCCycleRate );
	//m_slider_PALstealer	->SetValue( opts.PALCycleSteal );
	m_slider_RegionMODES->SetValue( opts.RegionMODES );

	//SetNTSCcycleSliderMsg();
	//SetPALcycleSliderMsg();
	SetRegionMODESliderMsg();

g_Conf->EnableSpeedHacks = SHconf.SpeedHacks ;
	Layout();
}

void Panels::ValDanX_KERNELPanel::Apply()
{
Pcsx2Config::GSOptions& gsconf( g_Conf->EmuOptions.GS );
Pcsx2Config::SpeedhackOptions& SHconf( g_Conf->EmuOptions.Speedhacks );
Pcsx2Config::ValDanX_KERNELOptions& opts( g_Conf->EmuOptions.ValDanX_KERNEL );

	//opts.NTSCCycleRate		= m_slider_NTSCcycle->GetValue();
	//opts.PALCycleSteal		= m_slider_PALstealer->GetValue();
	opts.RegionMODES 		= m_slider_RegionMODES->GetValue();

	for (int i = 0; i < NUM_OF_VALDANX_KERNEL; i++)
    {
        if (m_checkbox[i]->GetValue())
        {
            opts.bitset |= (1 << i);
        }
        else
        {
            opts.bitset &= ~(1 << i);
        }
	}
}
void Panels::ValDanX_KERNELPanel::AppStatusEvent_OnSettingsApplied()
{
	const Pcsx2Config::ValDanX_KERNELOptions& opts( g_Conf->EmuOptions.ValDanX_KERNEL );
	for( int i=0; i<NUM_OF_VALDANX_KERNEL; ++i )
	{
		m_checkbox[i]->SetValue( !!(opts.bitset & (1 << i)) );
	}
	AppStatusEvent_OnSettingsApplied( g_Conf->EmuOptions.ValDanX_KERNEL );
}
/*void Panels::ValDanX_KERNELPanel::NTSCCycleRate_Scroll(wxScrollEvent &event)
{
    SetNTSCcycleSliderMsg();
	event.Skip();
}

void Panels::ValDanX_KERNELPanel::PALCycleRate_Scroll(wxScrollEvent &event)
{
    SetPALcycleSliderMsg();
	event.Skip();
}*//*
void Panels::ValDanX_KERNELPanel::RegionMODE_Scroll(wxScrollEvent &event)
{
    SetRegionMODESliderMsg();
	event.Skip();
}
void Panels::ValDanX_KERNELPanel::Slider_Click(wxScrollEvent &event) {
	wxSlider* slider = (wxSlider*) event.GetEventObject();
	int value = slider->GetValue();
	int eventType = event.GetEventType();
	if (eventType == wxEVT_SCROLL_PAGEUP || eventType == wxEVT_SCROLL_LINEUP) {
		if (value > slider->GetMin()) {
			slider->SetValue(value-1);
		}
	}
	else if (eventType == wxEVT_SCROLL_TOP) {
		slider->SetValue(slider->GetMin());
	}
	else if (eventType == wxEVT_SCROLL_PAGEDOWN || eventType == wxEVT_SCROLL_LINEDOWN) {
		if (value < slider->GetMax()) {
			slider->SetValue(value+1);
		}
	}
	else if (eventType == wxEVT_SCROLL_BOTTOM) {
		slider->SetValue(slider->GetMax());
	}

	event.Skip();
}
void Panels::ValDanX_KERNELPanel::AppStatusEvent_DefaultSpeed( wxCommandEvent& evt )
{
		m_checkbox[0]->SetValue( false );
		m_checkbox[1]->SetValue( false );
		m_checkbox[2]->SetValue( false );
		m_checkbox[3]->SetValue( true );
		m_checkbox[4]->SetValue( true  );
		m_checkbox[5]->SetValue( false  );
}
void Panels::ValDanX_KERNELPanel::AppStatusEvent_DefaultUnSpeed( wxCommandEvent& evt )
{
		m_checkbox[0]->SetValue( false );
		m_checkbox[1]->SetValue( false );
		m_checkbox[2]->SetValue( false );
		m_checkbox[3]->SetValue( true  );
		m_checkbox[4]->SetValue( true  );
		m_checkbox[5]->SetValue( true  );

}
void Panels::ValDanX_KERNELPanel::AppStatusEvent_DefaultWarning( wxCommandEvent& evt )
{
    	m_checkbox[0]->SetValue( false );
		m_checkbox[1]->SetValue( false );
		m_checkbox[2]->SetValue( true  );
		m_checkbox[3]->SetValue( true  );
		m_checkbox[4]->SetValue( true  );
		m_checkbox[5]->SetValue( true  );
}*/